//
//  VCOfListenFM.m
//  LCOnePsychology
//
//  Created by li cheng on 16/9/2.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "VCOfListenFM.h"
#import "BaseTableView.h"
#import "BaseCollectionViewCell.h"
#import "BaseCollectionView.h"
#import "MJMacro.h"
#import "CVCellOfFirstRowListenFM.h"
#import "TVCellOfFirstRowListenFM.h"
#import "BaseView.h"
#import "TVCellOfSecondRowListenFM.h"
#import "CVCellOfListenFM.h"
#import "ListenFMRootClass.h"
#import "ListenFMData.h"
#import "ListenFMCategory.h"
#import "ListenFMHot_Fm.h"
#import "ListenFMNew_Fm.h"
#import "ListenFMNew_Lesson.h"
#import "ZWYNetTool.h"
#import "UIImageView+WebCache.h"
#import "VCOfClickButtonInListenFM.h"
#import "VCOfListenFMPlayInterface.h"
#import "ClickButtonInListenFMRootClass.h"
#import "ClickButtonInListenFMData.h"
@interface VCOfListenFM ()<UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) ListenFMRootClass *listenFMRootClass;
@property (nonatomic, strong) BaseTableView *listenFMTableView;
@property (nonatomic, strong) BaseCollectionView *choiceButtonOfCollectionView;
@property (nonatomic, strong) ClickButtonInListenFMRootClass *clickButtonInListenFMRootClassOfFM;
@property (nonatomic, strong) ClickButtonInListenFMRootClass *clickButtonInListenFMRootClassOfLesson;
@end

@implementation VCOfListenFM

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 数据处理
    [self handleData];
    // 设置返回按钮
    [self handleBackButton];
    // 创建 tableView
    [self createTableView];
}
#pragma mark - 数据解析
- (void)handleData{
    [ZWYNetTool GET:@"http://yiapi.xinli001.com/v2/yi/fm-home-list.json" andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _listenFMRootClass = [ListenFMRootClass modelDic:result];
        [self.listenFMTableView reloadData];
        [self.choiceButtonOfCollectionView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
    [ZWYNetTool GET:@"http://yiapi.xinli001.com/v2/yi/fm-broadcast-list.json?flag=fm&offset=0&limit=10&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&sign=f53c5d99180cb011e312dcf3f300733d&_version=4.0&token=0d4eeb8732f9ae2c73401002570ceb93" andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _clickButtonInListenFMRootClassOfFM = [ClickButtonInListenFMRootClass modelDic:result];
        [self.listenFMTableView reloadData];
        [self.choiceButtonOfCollectionView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
    [ZWYNetTool GET:@"http://yiapi.xinli001.com/v2/yi/fm-broadcast-list.json?flag=lesson&offset=0&limit=10&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&sign=f2f24bf27290ea47427362ec4b7ae45b&_version=4.0&token=0d4eeb8732f9ae2c73401002570ceb93" andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _clickButtonInListenFMRootClassOfLesson = [ClickButtonInListenFMRootClass modelDic:result];
        [self.listenFMTableView reloadData];
        [self.choiceButtonOfCollectionView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];

}
#pragma mark - 设置返回按钮
- (void)handleBackButton{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"< 返回" style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
}
#pragma mark - 返回的实现
- (void)back:(UIBarButtonItem *)barButton{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
#pragma mark - 创建tableView
- (void)createTableView{
    self.listenFMTableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
    self.listenFMTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_listenFMTableView];
    self.listenFMTableView.delegate = self;
    self.listenFMTableView.dataSource = self;
    [self.listenFMTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    [self.listenFMTableView registerClass:[TVCellOfFirstRowListenFM class] forCellReuseIdentifier:@"TVCellOfFirstRowListenFM"];
    [self.listenFMTableView registerNib:[UINib nibWithNibName:@"TVCellOfSecondRowListenFM" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TVCellOfSecondRowListenFM"];
    // 创建 CollectionView button
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.choiceButtonOfCollectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 4) collectionViewLayout:layout];
    self.choiceButtonOfCollectionView.backgroundColor = [UIColor whiteColor];
    layout.itemSize = CGSizeMake(self.choiceButtonOfCollectionView.bounds.size.width / 4, _choiceButtonOfCollectionView.bounds.size.height / 2);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.listenFMTableView.tableHeaderView = self.choiceButtonOfCollectionView;
    self.choiceButtonOfCollectionView.delegate = self;
    self.choiceButtonOfCollectionView.dataSource = self;
    [self.choiceButtonOfCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
    [self.choiceButtonOfCollectionView registerNib:[UINib nibWithNibName:@"CVCellOfListenFM" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"CVCellOfListenFM"];
    
}
#pragma mark - tableView 代理方法 section 数量的确定
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
#pragma mark - tableView 代理方法 row 数量的确定
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    } else {
        return 3;
    }
}
#pragma mark - tableView 代理方法 cell 的赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        TVCellOfFirstRowListenFM *cell = [tableView dequeueReusableCellWithIdentifier:@"TVCellOfFirstRowListenFM"];
        cell.backgroundColor = [UIColor orangeColor];
        return cell;
    } else {
        TVCellOfSecondRowListenFM *cell = [tableView dequeueReusableCellWithIdentifier:@"TVCellOfSecondRowListenFM"];
        if(indexPath.section == 1) {
            [cell.coverImageView sd_setImageWithURL:[NSURL URLWithString:_listenFMRootClass.data.myNew_fm[indexPath.row].cover] placeholderImage:[UIImage imageNamed:@"JustAMoment"]];
            cell.titleLabel.text = _listenFMRootClass.data.myNew_fm[indexPath.row].title;
            cell.speakLabel.text = _listenFMRootClass.data.myNew_fm[indexPath.row].speak;
            cell.viewnumLabel.text = _listenFMRootClass.data.myNew_fm[indexPath.row].viewnum;
        }
        if (indexPath.section == 2) {
            [cell.coverImageView sd_setImageWithURL:[NSURL URLWithString:_listenFMRootClass.data.myNew_lesson[indexPath.row].cover] placeholderImage:[UIImage imageNamed:@"JustAMoment"]];
            cell.titleLabel.text = _listenFMRootClass.data.myNew_lesson[indexPath.row].title;
            cell.speakLabel.text = _listenFMRootClass.data.myNew_lesson[indexPath.row].speak;
            cell.viewnumLabel.text = _listenFMRootClass.data.myNew_lesson[indexPath.row].viewnum;
        }
        return cell;
    }
}
#pragma mark - tableView cell 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        VCOfListenFMPlayInterface *vcOfListenFMPlayInterface = [VCOfListenFMPlayInterface defaultManager];
        vcOfListenFMPlayInterface.numberOfSelectNextSong = indexPath.row + 1;
        vcOfListenFMPlayInterface.numberOfSelectLastSong = indexPath.row - 1;
        vcOfListenFMPlayInterface.clickButtonInListenFMRootClass = _clickButtonInListenFMRootClassOfFM;
        vcOfListenFMPlayInterface.urlString = _listenFMRootClass.data.myNew_fm[indexPath.row].url;
        vcOfListenFMPlayInterface.coverString = _listenFMRootClass.data.myNew_fm[indexPath.row].cover;
        vcOfListenFMPlayInterface.titleString = _listenFMRootClass.data.myNew_fm[indexPath.row].title;
        vcOfListenFMPlayInterface.speakString = _listenFMRootClass.data.myNew_fm[indexPath.row].speak;
        [self presentViewController:vcOfListenFMPlayInterface animated:YES completion:^{
        }];
    }
    if (indexPath.section == 2) {
        VCOfListenFMPlayInterface *vcOfListenFMPlayInterface = [VCOfListenFMPlayInterface defaultManager];
        vcOfListenFMPlayInterface.numberOfSelectNextSong = indexPath.row + 1;
        vcOfListenFMPlayInterface.numberOfSelectLastSong = indexPath.row - 1;
        vcOfListenFMPlayInterface.clickButtonInListenFMRootClass = _clickButtonInListenFMRootClassOfLesson;
        vcOfListenFMPlayInterface.urlString = _listenFMRootClass.data.myNew_lesson[indexPath.row].url;
        vcOfListenFMPlayInterface.coverString = _listenFMRootClass.data.myNew_lesson[indexPath.row].cover;
        vcOfListenFMPlayInterface.titleString = _listenFMRootClass.data.myNew_lesson[indexPath.row].title;
        vcOfListenFMPlayInterface.speakString = _listenFMRootClass.data.myNew_lesson[indexPath.row].speak;
        [self presentViewController:vcOfListenFMPlayInterface animated:YES completion:^{
        }];
    }
}
#pragma mark - collectionVeiw 代理方法 item 的数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSLog(@"%ld", _listenFMRootClass.data.category.count);
    return _listenFMRootClass.data.category.count;
}
#pragma mark - CollectionView cell 的赋值
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CVCellOfListenFM *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CVCellOfListenFM" forIndexPath:indexPath];
    [cell.coverImageView sd_setImageWithURL:[NSURL URLWithString:_listenFMRootClass.data.category[indexPath.row].cover] placeholderImage:[UIImage imageNamed:@"JustAMoment"]];
    cell.nameLabel.text = _listenFMRootClass.data.category[indexPath.row].name;
    return cell;
}
#pragma mark - collection cell 的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    VCOfClickButtonInListenFM *vcOfClickButtonInListenFM = [VCOfClickButtonInListenFM defaultManager];
    [self.navigationController pushViewController:vcOfClickButtonInListenFM animated:YES];
    vcOfClickButtonInListenFM.stringOfURL = [NSString stringWithFormat:@"http://yiapi.xinli001.com/v2/yi/fm-category-broadcast-list.json?category_id=%@&offset=0&limit=10&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&sign=6e9078d5846e9b51cb5b739d99b7f069&_version=4.0&token=0d4eeb8732f9ae2c73401002570ceb93", _listenFMRootClass.data.category[indexPath.row].ID];
}
#pragma mark - row 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return SCREEN_HEIGHT / 4;
    } else {
        return SCREEN_HEIGHT * 0.15;
    }
}
#pragma mark - 我得写了这个方法才能有简单的用 UIView 去自定义分区尾视图
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
   return @"热门推荐";
}
#pragma mark - section 分区尾
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    BaseView *view = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    UIButton *buttonMore = [UIButton buttonWithType:UIButtonTypeSystem];
    buttonMore.frame = view.bounds;
    NSString *stringTitle = [NSString string];
    switch (section) {
        case 0:
        {
            buttonMore.tag = 1000;
            stringTitle = @"更多热门推荐";
        }
            break;
        case 1:{
            buttonMore.tag = 1001;
            stringTitle = @"更多FM";
        }
            break;
        case 2:{
            buttonMore.tag = 1002;
            stringTitle = @"更多心理课";
        }
            break;
        default:{
            return nil;
        }
            break;
    }
    [buttonMore setTitle:stringTitle forState:UIControlStateNormal];
    [buttonMore addTarget:self action:@selector(clickButtonMore:) forControlEvents:UIControlEventTouchUpInside];
    buttonMore.backgroundColor = [UIColor whiteColor];
    [view addSubview:buttonMore];
    return view;
}
#pragma mark - 点击 buttonMore 按钮的实现方法
- (void)clickButtonMore:(UIButton *)button{
    VCOfClickButtonInListenFM *vcOfClickButtonInListenFM = [VCOfClickButtonInListenFM defaultManager];
    [self.navigationController pushViewController:vcOfClickButtonInListenFM animated:YES];
    switch (button.tag) {
        case 1000:{
            vcOfClickButtonInListenFM.stringOfURL = @"http://yiapi.xinli001.com/v2/yi/fm-broadcast-list.json?flag=hot&offset=0&limit=10&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&sign=f53c5d99180cb011e312dcf3f300733d&_version=4.0&token=0d4eeb8732f9ae2c73401002570ceb93";
        }
            break;
        case 1001:{
            vcOfClickButtonInListenFM.stringOfURL = @"http://yiapi.xinli001.com/v2/yi/fm-broadcast-list.json?flag=fm&offset=0&limit=10&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&sign=f53c5d99180cb011e312dcf3f300733d&_version=4.0&token=0d4eeb8732f9ae2c73401002570ceb93";
        }
            break;
        case 1002:{
            vcOfClickButtonInListenFM.stringOfURL = @"http://yiapi.xinli001.com/v2/yi/fm-broadcast-list.json?flag=lesson&offset=0&limit=10&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&sign=f2f24bf27290ea47427362ec4b7ae45b&_version=4.0&token=0d4eeb8732f9ae2c73401002570ceb93";
        }
            break;
        default:
            break;
    }
}
#pragma mark - section Footer 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 44;
}
#pragma mark - 写这个方法,为了下面去自定义sectionHeaderView
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"热门推荐";
}
#pragma mark - section Header 自定义分区头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    BaseView *sectionHeaderView = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    sectionHeaderView.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
    UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(10, 5, 3, 20)];
    blueView.backgroundColor = [UIColor blueColor];
    [sectionHeaderView addSubview:blueView];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH / 2, 30)];
    NSString *stringTitle = [NSString string];
    switch (section) {
        case 0:
        {
            stringTitle = @"热门推荐";
        }
            break;
        case 1:{
            stringTitle = @"最新FM";
        }
            break;
        case 2:{
            stringTitle = @"最新心理课";
        }
            break;
        default:{
            return nil;
        }
            break;
    }
    titleLabel.text = stringTitle;
    [sectionHeaderView addSubview:titleLabel];
    return sectionHeaderView;
}
#pragma mark - section Header 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}
#pragma mark - 其他, 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
